GVPT Maths Camp

Data Visualisation I

Learning objectives for today

  1. Introduction to R

  2. Create your first plot in R

  3. Test your hypotheses using informative data visualizations

The Research Process

Source: R4DS

Data visualization

  • A great way to get to know your data

  • Critical to communicate your findings

R basics

R code:

1 + 2
[1] 3

Functions:

sum(1, 2)
[1] 3

EXERCISE

  1. Open up RStudio.
  2. Using the console, find the summation of 45, 978, and 121.
  3. What is 67 divided by 6?
  4. What is the square root of 894? HINT: use sqrt().

CHECK YOUR ANSWERS

Using the consol, find the summation of 45, 978, and 121.

sum(45, 978, 121)
[1] 1144

Or:

45 + 978 + 121
[1] 1144

What is 67 divided by 6?

67 / 6
[1] 11.16667

What is the square root of 894?

sqrt(894)
[1] 29.89983

R packages

Packages are collections of R functions, data, and compiled code in a well-defined format.

# Install the relevant package(s)
install.packages("tidyverse")

# Load the packages in current session
library(tidyverse)

EXERCISE

  1. Open up RStudio.
  2. Using the consol, install the tidyverse packages.
install.packages("tidyverse")
  1. Load these packages in your current session
library(tidyverse)

RStudio Projects

For your sanity’s sake, for your co-author’s sanity’s sake

Keeps everything:

  • Organised

  • Reproducible

  • Sustainable

EXERCISE

  1. In the RStudio console, type in getwd() to see where you are on your computer.

EXERCISE

  1. Create a new RStudio project for this Maths Camp:

Source: R4DS

EXERCISE

  1. Create a new RStudio project for this Maths Camp:

Source: R4DS

EXERCISE

  1. Create a new RStudio project for this Maths Camp:

Source: R4DS

Data visualisation

From R4DS - Data Visualization:

Do cars with big engines use more fuel than cars with small engines?

R4DS

This session will borrow (read: steal) heavily from Hadley Wickham’s R for Data Science book.

  • The. Best. Resource.

  • Hadley Wickham is one of the lead authors of the tidyverse. He created ggplot through his PhD dissertation.

Source: R4DS

Skipping to the end

How did we do this?

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point(mapping = aes(colour = class)) + 
  geom_smooth(method = "lm") + 
  theme(
    legend.position = "bottom",
    panel.grid = element_blank(),
    panel.background = element_blank(),
    plot.title.position = "plot",
    plot.title = element_text(face = "bold")
  ) + 
  labs(
    title = "Engine displacement and highway miles per gallon",
    subtitle = "Values for seven different classes of cars",
    x = "Engine displacement (L)",
    y = "Highway miles per gallon"
  ) + 
  scale_color_colorblind()

Load relevant packages and data

# Load the relevant packages
library(tidyverse)

# Load the data
mpg
manufacturer model displ year cyl
audi a4 1.8 1999 4
audi a4 1.8 1999 4
audi a4 2.0 2008 4
audi a4 2.0 2008 4
audi a4 2.8 1999 6
audi a4 2.8 1999 6

EXERCISE


Learn more about this data set by typing ?mpg into your console.

The mpg data set

glimpse(mpg)
Rows: 234
Columns: 11
$ manufacturer <chr> "audi", "audi", "audi", "audi", "audi", "audi", "audi", "…
$ model        <chr> "a4", "a4", "a4", "a4", "a4", "a4", "a4", "a4 quattro", "…
$ displ        <dbl> 1.8, 1.8, 2.0, 2.0, 2.8, 2.8, 3.1, 1.8, 1.8, 2.0, 2.0, 2.…
$ year         <int> 1999, 1999, 2008, 2008, 1999, 1999, 2008, 1999, 1999, 200…
$ cyl          <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8, …
$ trans        <chr> "auto(l5)", "manual(m5)", "manual(m6)", "auto(av)", "auto…
$ drv          <chr> "f", "f", "f", "f", "f", "f", "f", "4", "4", "4", "4", "4…
$ cty          <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 17, 17, 1…
$ hwy          <int> 29, 29, 31, 30, 26, 26, 27, 26, 25, 28, 27, 25, 25, 25, 2…
$ fl           <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p…
$ class        <chr> "compact", "compact", "compact", "compact", "compact", "c…

The mpg data set

A couple of useful variables:

  • displ: engine displacement, in litres

  • hwy: highway miles per gallon

Set up your plot

An empty canvas!

library(ggplot2)
library(ggthemes)

ggplot(data = mpg)

Map your aesthetics

ggplot(data = mpg, mapping = aes(x = displ, y = hwy))

Add in your cars

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point()

Let’s look at groups in the data

  • We are not restricted to looking at only two interesting elements of our data.

  • You can use visual elements or aesthetics (aes) to communicate many dimensions in your data.

  • Let’s look at a categorical variable: the class of car (SUV, 2 seater, pick up truck, etc.).

  • Look for meaningfully defined groups.

Let’s look at groups in the data

ggplot(data = mpg, mapping = aes(x = displ, y = hwy, colour = class)) + 
  geom_point()

Add more information

ggplot(data = mpg, mapping = aes(x = displ, y = hwy, colour = class)) + 
  geom_point() + 
  geom_smooth(method = "lm") + 
  scale_color_colorblind()

Look at the relationship across all cars

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point(mapping = aes(colour = class)) + 
  geom_smooth(method = "lm") + 
  scale_color_colorblind()

Add useful titles and labels

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point(mapping = aes(colour = class)) + 
  geom_smooth(method = "lm") + 
  labs(
    title = "Engine displacement and highway miles per gallon",
    subtitle = "Values for seven different classes of cars",
    x = "Engine displacement (L)",
    y = "Highway miles per gallon"
  ) + 
  scale_color_colorblind()

Add useful titles and labels

Summary

This session you:

  1. Set up your data science tools

  2. Plotted complex data in an engaging way

  3. Discovered interesting relationships in the data

  4. Connected these relationships or trends to your expectations (or hypotheses about the data)

HOMEWORK

In the final session, you will apply the skills you will learn over the next few days to a problem that interests you. To prepare for this, you need to find a data set that:

  1. Is relevant to your research interests,

  2. Contains continuous and discrete variables.